package com.china08.yxyapi.api.manage.tec.office.innernotice;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.DefaultImageUtil;

/**
 * 老师发送内部通知 已读列表查询
 * 
 */
@CrossOrigin
@RestController("TeaInnerNoticeReceiveReadSrhController")
public class TeaInnerNoticeReceiveReadSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @RequestMapping(value = "/api_yw/manage/tea/office/innerNotice/receive/read", method = RequestMethod.POST)
    public Page<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel) {
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size, Direction.DESC, "createdDate");
        // 当前教育局id
        String schoolId = getProfile(request, response).getSchoolId();
        // 当前用户的获取
        String userId = getProfile(request, response).getId();
        // 未删除状态
        int isDelete = 0;

        List<YxyNoticeInner> page = yxyNoticeInnerRepository.findBySchoolIdAndReceiverUserIdAndReceiverIsDeleteAndReceiver(schoolId, userId, isDelete, 1, pageable);
        List<RespModel> respModelLst = new ArrayList<RespModel>();
        RespModel respModel = null;

        // ###
        // 学校昵称赋值 注释掉：未使用
        // String schoolNick = "";
        // if (page.get(0).getSchoolNick() != null) {
        // schoolNick = page.get(0).getSchoolNick();
        // }
        // if ("".equals(schoolNick)) {
        // Map<String, String> map2 = restTemplate.getForObject(yxyApiYxUrl +
        // "/comm/school/" + schoolId, Map.class);
        // schoolNick = map2.get("schoolNick") == null ? "" :
        // map2.get("schoolNick");
        // }

        for (YxyNoticeInner yxyNoticeInner : page) {
            respModel = new RespModel();
            respModel.id = yxyNoticeInner.getId();
            respModel.userId = yxyNoticeInner.getUserId();
            respModel.userNick = yxyNoticeInner.getUserNick();
            String avatar = yxyNoticeInner.getAvatar();
            avatar = DefaultImageUtil.default1(avatar);
            respModel.avatar = avatar;
            respModel.title = yxyNoticeInner.getTitle();
            respModel.content = yxyNoticeInner.getContent();

            respModel.createdDate = DateToolUtils.format(yxyNoticeInner.getCreatedDate(), DateToolUtils.FORMAT_SHORT_YMD);
            respModel.schoolId = yxyNoticeInner.getSchoolId();
            respModel.schoolNick = "";
            respModel.image = yxyNoticeInner.getImage();
            respModel.attachmentName = yxyNoticeInner.getAttachmentName();
            respModel.attachmentPath = yxyNoticeInner.getAttachmentPath();
            respModelLst.add(respModel);
        }
        int totalElements = yxyNoticeInnerRepository.countBySchoolIdAndReceiverUserIdAndReceiverIsDeleteAndReceiverIsRead(schoolId, userId, isDelete, 1);
        Page<RespModel> respModelPage = new PageImpl<RespModel>(respModelLst, pageable, totalElements);
        return respModelPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel {
        // 消息id
        public String id;
        // 用户id
        public String userId;
        // 用户昵称
        public String userNick;
        // 头像
        public String avatar;
        // 创建时间
        public String createdDate;
        // 标题
        public String title;
        // 内容
        public String content;
        // 学校id
        public String schoolId;
        // 学校昵称
        public String schoolNick;
        // 图片列表
        public List<String> image;
        // 文件名
        public String attachmentName;
        // 文件路径
        public String attachmentPath;
    }
}
